//@version=5

indicator(title="PunkChainer™ ToolKit 4.0 [🧊 PunkAlgo 3.0]", overlay=true, precision=4, linktoseries=true, max_bars_back=1000, 
 max_lines_count=500)



// New Golden Fibonacci

///                     
//      Pre-DEFINE     
//                     
//---------------------------------------------------
var COLOR_TRANSP        = color.new(#ffffff,100)
var COLOR_BLACK         = color.new(#000000,0)  
//---------------------------------------------------
var basicgr             = "🧊 PunkAlgo Smart Fibonacci" 
var fb_tooltip     = "\nDepth ; The minimum number of bars that will be taken into account when calculating the indicator.\n\nDeviation ; a multiplier that affects how much the price should deviate from the previous pivot in order for the bar to become a new pivot.\n"


//     INPUT                                                                                                                                                                     
fb_depth            = input.int   (10   ,minval=1,maxval=20     ,title = "How Far   "                     ,inline='FBR1'       ,group=basicgr)
fb_dev_ratio        = input.float (3.0  ,minval=1,step=0.5      ,title = "Wave Size "                     ,inline='FBR2'       ,group=basicgr   ,tooltip=fb_tooltip)
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
fb_dev_threshold    = (ta.atr(20)/close) * 100 * fb_dev_ratio
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------


fb236=input(false,"236 ",inline='fbl'   ,group=basicgr)
fb382=input(true ,"382 ",inline='fbl'   ,group=basicgr)
fb500=input(true ,"500 ",inline='fbl'   ,group=basicgr)
fb618=input(true ,"618 ",inline='fbl'   ,group=basicgr)
fb786=input(false,"786 ",inline='fbl'   ,group=basicgr)
fb886=input(false,"886" ,inline='fbl'   ,group=basicgr)

//---------------------------------------------------------------------------------------------------------------------------------------------------
view_gr = '🔴🟠🟡🟢🔵'

draw_pmark_c       = input.color (color.new(#ffffff,0)        ,""                       ,inline='pmark'    ,group=view_gr)
draw_pmark         = input.bool  (false                          ,'◯  Pivot Point'        ,inline='pmark'    ,group=view_gr)

draw_tline_c       = input.color (color.new(#ffffff,0)        ,""                       ,inline='tline'    ,group=view_gr)
draw_tline         = input.bool  (false                          ,'-- Trend Line'         ,inline='tline'    ,group=view_gr)

fb_guide_color      = input.color (color.new(#ffff00,0)       ,title = ""               ,inline='retlg'    ,group=view_gr )
fb_guide_draw       = input.bool  (false                         ,'▮ Retracement Meter '     ,inline='retlg'    ,group=view_gr)


fb_color2           = input.color (color.new(#ffa726,0)       ,title = "Basic Color Scheme"    ,inline='color1'    ,group=view_gr )
fb_color1           = input.color (color.new(#b2b5be,0)       ,title = ""                      ,inline='color1'    ,group=view_gr )
fb_label_position   = input.int   (23                           ,title = "Ruller position (X-axis Offset)"       ,inline='position'    ,group=view_gr )
draw_simplelabel   = input.bool  (false                          ,' Extended Info. (Price, Delta Rate)'  ,inline=''    ,group=view_gr)
//---------------------------------------------------------------------------------------------------------------------------------------------------




//---------------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------------
[k_volume,k_open,k_close,k_high,k_low,k_hlc3,k_hlcc4,k_ohlc4,k_hl2,k_close_mintick,k_trtrue,k_atr14,k_atr14_mintick,k_rsipvt14] 
     = request.security(syminfo.tickerid ,'', [volume,open,close,high,low,hlc3,hlcc4,ohlc4,hl2,str.tostring(close,format.mintick),ta.tr(true),ta.atr(14),str.tostring(ta.atr(14),format.mintick),ta.rsi(ta.pvt,14)] )






//                                                                                                                                                                                                                     
//       Background                                                                                                                                                                                                    
//                                                                                                                                                                                                                   

//                                                                                                                                                                                                                     
//      Fibonacci Retracement  - Logic Performance Tuned & Re-Designed Fresh-Drawing, Automatic Line on/off                                                                                                            
//                                                                                                                                                                                                                     


// pre-define --------------------------------------------------------------------------------------
var line  fb_lineLast           = na
var label fb_label_start_last   = na
var label fb_label_end_last     = na
var int   fb_iLast              = 0
var int   fb_iPrev              = 0
var float fb_pLast              = 0
var fb_isHighLast               = false     // otherwise the last pivot is a low pivot
// pivot --------------------------------------------------------------------------------------
high_none   = ta.highest(high,fb_depth) > nz(high[fb_depth/2])
fb_iH       = high_none ?   int(na) : bar_index[fb_depth/2]    
fb_pH       = high_none ? float(na) :   nz(high[fb_depth/2])
low_none    = ta.lowest (low ,fb_depth) < nz(low [fb_depth/2])
fb_iL       = low_none  ?   int(na) : bar_index[fb_depth/2]
fb_pL       = low_none  ? float(na) :   nz(low [fb_depth/2])
//--------------------------------------------------------------------------------------
calc_dev(_base_price, _price) => 100 * (_price - _base_price) / _price
//--------------------------------------------------------------------------------------





pivotFound(dev, isHigh, index, price) =>
    if fb_isHighLast == isHigh and not na(fb_lineLast) 
        // same direction
        if fb_isHighLast ? price > fb_pLast : price < fb_pLast
            line.set_xy2(fb_lineLast, index, price)
            label.set_xy(fb_label_end_last, index, price)
            [fb_lineLast, fb_isHighLast,fb_label_start_last,fb_label_end_last]
        else
            [line(na), bool(na), label(na), label(na)]
    else // reverse the direction (or create the very first line)
        if math.abs(dev) > fb_dev_threshold 
            // price move is significant
            id_fb_line = line.new(fb_iLast, fb_pLast, index, price, color=color.new(draw_tline_c,draw_tline?0:100), width=1, style=line.style_dashed)       // ------  FBR start -> end ; slopped line
            id_fb_label_start = label.new(fb_iLast, fb_pLast ,color=COLOR_TRANSP, size=size.huge ,style=label.style_label_center, textcolor=color.new(draw_pmark_c,draw_pmark?0:100)  ,text= '◯' )    
            id_fb_label_end   = label.new(index   , price    ,color=COLOR_TRANSP, size=size.huge ,style=label.style_label_center, textcolor=color.new(draw_pmark_c,draw_pmark?0:100)  ,text= '◯' )    
            [id_fb_line, isHigh, id_fb_label_start, id_fb_label_end]
        else
            [line(na), bool(na),label(na),label(na)]
//--------------------------------------------------------------------------------------
fb_new_line = false
if not na(fb_iH) 
    fb_dev = calc_dev(fb_pLast, fb_pH)
    [fb_id, fb_isHigh, fb_la_s_id, fb_la_e_id] = pivotFound(fb_dev, true, fb_iH, fb_pH)
    if not na(fb_id)
        if fb_id != fb_lineLast
            line.delete (fb_lineLast)
            label.delete(fb_label_start_last)
            label.delete(fb_label_end_last)
            fb_new_line     := true
        fb_lineLast         := fb_id
        fb_label_start_last := fb_la_s_id
        fb_label_end_last   := fb_la_e_id
        fb_isHighLast       := fb_isHigh
        fb_iPrev            := fb_iLast
        fb_iLast            := fb_iH
        fb_pLast            := fb_pH
else if not na(fb_iL) 
    fb_dev = calc_dev(fb_pLast, fb_pL)
    [fb_id, fb_isHigh, fb_la_s_id, fb_la_e_id] = pivotFound(fb_dev, false, fb_iL, fb_pL)
    if not na(fb_id)
        if fb_id != fb_lineLast
            line.delete (fb_lineLast)
            label.delete(fb_label_start_last)
            label.delete(fb_label_end_last)
            fb_new_line     := true
        fb_lineLast         := fb_id
        fb_label_start_last := fb_la_s_id
        fb_label_end_last   := fb_la_e_id                
        fb_isHighLast       := fb_isHigh
        fb_iPrev            := fb_iLast
        fb_iLast            := fb_iL
        fb_pLast            := fb_pL
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
fb_retracement (_show, _fib_level, _col ,_switch) =>
    _price   = fb_pLast + ((line.get_y1(fb_lineLast)-fb_pLast))* _fib_level
    _fbfb_id_line_show = false
    _position           = bar_index -  line.get_x2(fb_lineLast)
    _fbr_max_highest    = math.max( ta.highest(high, nz(math.max(1,_position),1))  , fb_pLast)
    _fbr_min_lowest     = math.min( ta.lowest (low , nz(math.max(1,_position),1))  , fb_pLast)
    _fbr_max            = fb_pLast < close ? _fbr_max_highest : fb_pLast > close  ? _fbr_min_lowest : fb_pLast
    _fbr_line_max       = fb_pLast < _price? _fbr_max_highest : fb_pLast > _price ? _fbr_min_lowest : fb_pLast
    _fbfb_id_line_show  := fb_pLast < _price and _price-(k_atr14/4) <= _fbr_line_max  ? true 
                         : fb_pLast > _price and _price+(k_atr14/4) >= _fbr_line_max  ? true 
                         : false 
    if _switch =='auto' and not na(fb_lineLast)
        fbfb_id_auto      = line.new(line.get_x2(fb_lineLast) ,_price ,bar_index + 6                    ,_price ,style=line.style_solid, color=color.new(_col,_fbfb_id_line_show?30:100) , width=1, extend=extend.none)    
        fbfb_id_ext_auto  = line.new(bar_index+fb_label_position-4,_price ,bar_index+fb_label_position+1  ,_price ,color=color.new(_col,0), width=1, extend=extend.none)     
        fb_label_auto     = label.new(x=bar_index+fb_label_position+1 ,y=_price, text=str.tostring(_fib_level,"0.000")+(draw_simplelabel ? ('    ' + str.tostring(_price, format.mintick) + '   ' + str.tostring(((_price - fb_pLast)/fb_pLast)*100,format.percent )):''),textcolor=color.new(_col,40) ,style=label.style_label_left  ,textalign=text.align_left , color=#00000000, size=size.normal)
        line.delete(fbfb_id_auto[1]     )
        line.delete(fbfb_id_ext_auto[1] )
        label.delete(fb_label_auto[1]   )
        if not _show 
            line.delete(fbfb_id_auto     )
            line.delete(fbfb_id_ext_auto )
            label.delete(fb_label_auto   )
    if _show and _switch ==''
        var fbfb_id      = line.new(fb_iLast, _price, bar_index, _price, color=color.new(_col,0) , width=1, extend=extend.none  ,style=line.style_solid )     //--- FBR | line = start to now 
        var fbfb_id_ext  = line.new(fb_iLast, _price, bar_index, _price, color=color.new(_col,0) , width=1, extend=extend.none)     //--- FBR | line = now to label position
        var fb_label     = label.new(x=bar_index ,y=_price, text=''  ,textcolor=COLOR_TRANSP ,style=label.style_label_left  ,textalign=text.align_left , color=#00000000, size=size.normal)
        if not na(fb_lineLast)
            line.set_xy1  (fbfb_id  ,_fib_level==1.0? line.get_x1(fb_lineLast):line.get_x2(fb_lineLast), _price)             
            line.set_xy2  (fbfb_id  ,_fib_level==1.0 or _fib_level==0.0? bar_index + fb_label_position-4 : bar_index + 6           , _price)                           
            line.set_color(fbfb_id  ,color.new(_col, _fib_level==0.0?0:_fib_level==1.0?0:_fbfb_id_line_show ? 30 : 100))
            // line.set_style(fbfb_id  ,_fib_level==0.0 or _fib_level==1.0 ? line.style_dashed : line.style_solid)
            //---------------------------------------------------------------------------------------------------    
            line.set_xy1   (fbfb_id_ext    ,bar_index+fb_label_position-4  ,_price)                        
            line.set_xy2   (fbfb_id_ext    ,bar_index+fb_label_position+1  ,_price)
            line.set_width (fbfb_id_ext    ,1)
            //---------------------------------------------------------------------------------------------------    
            label.set_xy        (fb_label ,bar_index+fb_label_position+1  ,_price)
            label.set_text      (fb_label ,str.tostring(_fib_level,"0.000")+(draw_simplelabel ? '    ' + str.tostring(_price, format.mintick) + '   ' + str.tostring(((_price - fb_pLast)/fb_pLast)*100,format.percent ):''))
            // label.set_tooltip   (fb_label ,str.tostring(((_price - fb_pLast)/fb_pLast)*100,format.percent ))
            label.set_textcolor (fb_label ,color.new(_col,30))    
            
            if _fib_level == 1.0 and fb_guide_draw == true
                var fbfb_box_max  = box.new  (bar_index, close   ,bar_index, fb_pLast, border_color=color.new(fb_guide_color,80) ,border_width=1, bgcolor=color.new(fb_guide_color,90) )
                var fbfb_line_max = line.new (bar_index, close   ,bar_index, fb_pLast, color=fb_guide_color ,width=2)
                var fbfb_line_min = line.new (bar_index, close   ,bar_index, fb_pLast, color=fb_guide_color ,width=2)
                var fbfb_box_ext  = box.new  (bar_index, close   ,bar_index, fb_pLast, border_color=color.new(fb_guide_color,80) ,border_width=0, bgcolor=color.new(fb_guide_color,70) )
                var fbfb_line_ext = line.new (bar_index, close   ,bar_index, fb_pLast, color=fb_guide_color ,width=3)
                //---------------------------------------------------------------------------------------------------    
                box.set_lefttop     (fbfb_box_max  ,bar_index+fb_label_position-3 ,_fbr_max_highest)
                box.set_rightbottom (fbfb_box_max  ,bar_index+fb_label_position-0 ,_fbr_min_lowest) 
                //---------------------------------------------------------------------------------------------------    
                line.set_xy1        (fbfb_line_max ,bar_index+fb_label_position-3 ,_fbr_max_highest)
                line.set_xy2        (fbfb_line_max ,bar_index+fb_label_position-0 ,_fbr_max_highest)
                line.set_xy1        (fbfb_line_min ,bar_index+fb_label_position-3 ,_fbr_min_lowest)
                line.set_xy2        (fbfb_line_min ,bar_index+fb_label_position-0 ,_fbr_min_lowest)
                //---------------------------------------------------------------------------------------------------    
                box.set_lefttop     (fbfb_box_ext  ,bar_index+fb_label_position-3 ,close)
                box.set_rightbottom (fbfb_box_ext  ,bar_index+fb_label_position-1 ,fb_pLast)
                //---------------------------------------------------------------------------------------------------    
                line.set_xy1        (fbfb_line_ext ,bar_index+fb_label_position-3 ,close)
                line.set_xy2        (fbfb_line_ext ,bar_index+fb_label_position-1 ,close)
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
fb_new = fb_pLast[1] != fb_pLast or fb_new_line or fb_iLast[1] != fb_iLast
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
fb_over_1618    = false
fb_over_1       = false
fb_over_0786    = false
fb_under_0      = false
fb_under_0382   = false
fb_under_0618   = false
fb_under_m1     = false
//-------------------------
fb_height       = line.get_y1(fb_lineLast) - fb_pLast

fb_k_low_cr     = ta.lowest (k_low,2)
fb_k_high_cr    = ta.highest(k_high,2)

fb_over_1618   := (fb_height < 0 and fb_k_low_cr < (fb_height  * 1.618) + fb_pLast  ) or (fb_height > 0 and fb_k_high_cr > (fb_height  * 1.618) + fb_pLast   ) 
fb_over_1      := (fb_height < 0 and fb_k_low_cr < line.get_y1(fb_lineLast)         ) or (fb_height > 0 and fb_k_high_cr > line.get_y1(fb_lineLast)          ) 
fb_over_0786   := (fb_height < 0 and fb_k_low_cr < (fb_height  * 0.786) + fb_pLast  ) or (fb_height > 0 and fb_k_high_cr > (fb_height  * 0.786) + fb_pLast   ) 
fb_under_0     := (fb_height < 0 and fb_k_low_cr > fb_pLast                         ) or (fb_height > 0 and fb_k_high_cr < fb_pLast                          )
fb_under_0382  := (fb_height < 0 and fb_k_low_cr > (fb_height  *-0.382) + fb_pLast  ) or (fb_height > 0 and fb_k_high_cr < (fb_height  *-0.382) + fb_pLast   )
fb_under_0618  := (fb_height < 0 and fb_k_low_cr > (fb_height  *-0.618) + fb_pLast  ) or (fb_height > 0 and fb_k_high_cr < (fb_height  *-0.618) + fb_pLast   )
fb_under_m1    := (fb_height < 0 and fb_k_low_cr > (fb_height  *-1    ) + fb_pLast  ) or (fb_height > 0 and fb_k_high_cr < (fb_height  *-1    ) + fb_pLast   ) 
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
fb_retracement (fb_under_m1   ,-1.618 ,fb_color1 ,'auto')
fb_retracement (fb_under_0618 ,-1.000 ,fb_color1 ,'auto')
fb_retracement (fb_under_0382 ,-0.618 ,fb_color1 ,'auto')
fb_retracement (fb_under_0    ,-0.382 ,fb_color1 ,'auto')
fb_retracement (true          ,-0.236 ,fb_color1 ,'auto')
fb_retracement (true          , 0.000 ,fb_color2 ,''    )
fb_retracement (fb236         , 0.236 ,fb_color1 ,''    )
fb_retracement (fb382         , 0.382 ,fb_color2 ,''    )
fb_retracement (fb500         , 0.500 ,fb_color2 ,''    )
fb_retracement (fb618         , 0.618 ,fb_color2 ,''    )
fb_retracement (fb786         , 0.786 ,fb_color1 ,''    )
fb_retracement (fb886         , 0.886 ,fb_color1 ,''    )
fb_retracement (true                          , 1.000 ,fb_color1 ,''    )
fb_retracement (fb_over_0786  , 1.272 ,fb_color1 ,'auto')
fb_retracement (fb_over_1     , 1.618 ,fb_color2 ,'auto')
fb_retracement (fb_over_1618  , 2.618 ,fb_color2 ,'auto')






// Volume Profile

vp_lookback = input.int(defval=80, title='Volume Lookback Depth [10-1000]', minval=10, maxval=1000, group="🧊 Punk Volume Profile")

vp_max_bars = input.int(defval=500, title='Number of Bars [10-500]', minval=10, maxval=500, group="🧊 Punk Volume Profile")

vp_bar_mult = input.int(defval=10, title='Bar Length Multiplier [10-100]', minval=10, maxval=100, group="🧊 Punk Volume Profile")

vp_bar_offset = input.int(defval=40, title='Bar Horizontal Offset [0-100]', minval=0, maxval=100, group="🧊 Punk Volume Profile")

vp_bar_width = input.int(defval=1, title='Bar Width [1-20]', minval=1, maxval=20, group="🧊 Punk Volume Profile")

vp_delta_type = input.string(defval='Both', title='Delta Type', options=['Both', 'Bullish', 'Bearish'], group="🧊 Punk Volume Profile")

vp_poc_show = input(defval=true, title='Show POC Line', group="🧊 Punk Volume Profile")

vp_vah_val_show = input(defval=false, title='Show VAH and VAL Lines', group="🧊 Punk Volume Profile")

vp_profile_show = input(defval=true, title='Show Volume Profile', group="🧊 Punk Volume Profile")  // added input switch

vp_bar_color = input(defval=color.new(color.gray, 60), title='Bar Color', group="🧊 Punk Volume Profile")

vp_poc_color = input(defval=color.new(#90bff9, 10), title='POC Color', group="🧊 Punk Volume Profile")

vp_vah_color = input(defval=color.new(color.white, 10), title='VAH Color', group="🧊 Punk Volume Profile")

vp_val_color = input(defval=color.new(color.white, 10), title='VAL Color', group="🧊 Punk Volume Profile")


float vp_Vmax = 0.0
int vp_VmaxId = 0
int vp_N_BARS = vp_max_bars

var int vp_first = time

vp_a_P = array.new_float(vp_N_BARS + 1, 0.0)
vp_a_V = array.new_float(vp_N_BARS, 0.0)
vp_a_D = array.new_float(vp_N_BARS, 0.0)
vp_a_W = array.new_int(vp_N_BARS, 0)

float vp_HH = ta.highest(high, vp_lookback)
float vp_LL = ta.lowest(low, vp_lookback)

if barstate.islast
    float vp_HL = (vp_HH - vp_LL) / vp_N_BARS
    for j = 1 to vp_N_BARS + 1 by 1
        array.set(vp_a_P, j - 1, vp_LL + vp_HL * j)
    for i = 0 to vp_lookback - 1 by 1
        int Dc = 0
        array.fill(vp_a_D, 0.0)
        for j = 0 to vp_N_BARS - 1 by 1
            float Pj = array.get(vp_a_P, j)
            if low[i] < Pj and high[i] > Pj and (vp_delta_type == 'Bullish' ? close[i] >= open[i] : vp_delta_type == 'Bearish' ? close[i] <= open[i] : true)
                float Dj = array.get(vp_a_D, j)
                float dDj = Dj + nz(volume[i])
                array.set(vp_a_D, j, dDj)
                Dc += 1
                Dc
        for j = 0 to vp_N_BARS - 1 by 1
            float Vj = array.get(vp_a_V, j)
            float Dj = array.get(vp_a_D, j)
            float dVj = Vj + (Dc > 0 ? Dj / Dc : 0.0)
            array.set(vp_a_V, j, dVj)
    vp_Vmax := array.max(vp_a_V)
    vp_VmaxId := array.indexof(vp_a_V, vp_Vmax)
    for j = 0 to vp_N_BARS - 1 by 1
        float Vj = array.get(vp_a_V, j)
        int Aj = math.round(vp_bar_mult * Vj / vp_Vmax)
        array.set(vp_a_W, j, Aj)

if barstate.isfirst
    vp_first := time
    vp_first
vp_change = ta.change(time)
vp_x_loc = timenow + math.round(vp_change * vp_bar_offset)

f_setup_bar(n) =>
    if vp_profile_show
        x1 = vp_VmaxId == n and vp_poc_show ? math.max(time[vp_lookback], vp_first) : timenow + math.round(vp_change * (vp_bar_offset - array.get(vp_a_W, n)))
        ys = array.get(vp_a_P, n)
        line.new(x1=x1, y1=ys, x2=vp_x_loc, y2=ys, xloc=xloc.bar_time, extend=extend.none, color=vp_VmaxId == n ? vp_poc_color : vp_bar_color, style=line.style_solid, width=vp_bar_width)

if barstate.islast
    for i = 0 to vp_N_BARS - 1 by 1
        f_setup_bar(i)

float total_volume = array.sum(vp_a_V)
int VAL_id = na
int VAH_id = na
float cumulative_volume = 0.0
for j = 0 to vp_N_BARS - 1 by 1
    cumulative_volume := cumulative_volume + array.get(vp_a_V, j)
    if na(VAL_id) and cumulative_volume > total_volume * 0.26
        VAL_id := j
    if na(VAH_id) and cumulative_volume > total_volume * 0.963
        VAH_id := j
        break

f_setup_line(n, line_color) =>
    x1 = math.max(time[vp_lookback], vp_first)
    ys = array.get(vp_a_P, n)
    line.new(x1=x1, y1=ys, x2=vp_x_loc, y2=ys, xloc=xloc.bar_time, 
     extend=extend.none, color=line_color, style=line.style_solid, 
     width=vp_bar_width)

if barstate.islast and vp_vah_val_show
    f_setup_line(VAH_id, vp_vah_color)
    f_setup_line(VAL_id, vp_val_color)

// ------------- EMA 55 / 200 --------------------------//
showEmas          = input(false, "Show EMAs", group="🧊 EMA Module")
srcEma1           = input(close, "Source EMA 1")
lenEma1           = input.int(55, "Length EMA 1", 1)
srcEma2           = input(close, "Source EMA 2")
lenEma2           = input.int(200, "Length EMA 2", 1)
ema55color        = input(#089981, "EMA Color 55", group="🧊 EMA Module")
ema200color        =input(#b22833, "EMA Color 200", group="🧊 EMA Module")

ema1      = ta.ema(srcEma1, lenEma1)
ema2      = ta.ema(srcEma2, lenEma2)
ema = ta.ema(close, 144)
emaBull = close > ema

plot(showEmas ? ema1 : na, "EMA 55", ema55color, 1)
plot(showEmas ? ema2 : na, "EMA 200", ema200color, 1)


// SMA Module

showEmas2 = input(false, "Show SMA", group="🧊 SMA Module")
len3 = input.int(200, minval=1, title="SMA length")
src3 = input(close, title="SMA source")
smacolor  = input(#ffffff, "SMA Color", group="🧊 SMA Module")
out3 = ta.sma(src3, len3)
plot(showEmas2 ? out3 : na, "SMA 200", smacolor, 1)

// Support and Resistances


enableSR   = input(false, "SR On/Off", group="🧊 Support and Resistances")
colorSup   = input(color.new(#00DBFF,20), "Support Color", group="Support and Resistances")
colorRes   = input(color.new(#9598a1,50), "Resistance Color", group="Support and Resistances")
strengthSR = input.int(2, "S/R Strength", 1, group="Support and Resistances")
lineStyle  = input.string("Dotted", "Line Style", ["Solid", "Dotted", "Dashed"], group="Support and Resistances")
lineWidth  = input.int(1, "S/R Line Width", 1, group="Support and Resistances")
useZones   = input(true, "Zones On/Off", group="Support and Resistances")
useHLZones = input(true, "High Low Zones On/Off", group="Support and ResistancesSR")
zoneWidth  = input.int(1, "Zone Width %", 0, tooltip="it's calculated using % of the distance between highest/lowest in last 300 bars", group="Support and Resistances")
expandSR   = input(true, "Expand Support and Resistances")
// Functions
percWidth(len, perc) => (ta.highest(len) - ta.lowest(len)) * perc / 100
// Get components
rb            = 10
prd           = 284
ChannelW      = 10
label_loc     = 55
style         = lineStyle == "Solid" ? line.style_solid : lineStyle == "Dotted" ? line.style_dotted : line.style_dashed
ph            = ta.pivothigh(rb, rb)
pl            = ta.pivotlow (rb, rb)
sr_levels     = array.new_float(21, na)
prdhighest    = ta.highest(prd)
prdlowest     = ta.lowest(prd)
cwidth        = percWidth(prd, ChannelW)
zonePerc      = percWidth(300, zoneWidth)
aas           = array.new_bool(41, true)
u1            = 0.0, u1 := nz(u1[1])
d1            = 0.0, d1 := nz(d1[1])
highestph     = 0.0, highestph := highestph[1]
lowestpl      = 0.0, lowestpl := lowestpl[1]
var sr_levs   = array.new_float(21, na)
label hlabel  = na, label.delete(hlabel[1])
label llabel  = na, label.delete(llabel[1])
var sr_lines  = array.new_line(21, na)
var sr_linesH = array.new_line(21, na)
var sr_linesL = array.new_line(21, na)
var sr_linesF = array.new_linefill(21, na)
var sr_labels = array.new_label(21, na)
if ph or pl
    for x = 0 to array.size(sr_levels) - 1
        array.set(sr_levels, x, na)
    highestph := prdlowest
    lowestpl := prdhighest
    countpp = 0
    for x = 0 to prd
        if na(close[x])
            break
        if not na(ph[x]) or not na(pl[x])
            highestph := math.max(highestph, nz(ph[x], prdlowest), nz(pl[x], prdlowest))
            lowestpl := math.min(lowestpl, nz(ph[x], prdhighest), nz(pl[x], prdhighest))
            countpp += 1
            if countpp > 40
                break
            if array.get(aas, countpp)
                upl = (ph[x] ? high[x + rb] : low[x + rb]) + cwidth
                dnl = (ph[x] ? high[x + rb] : low[x + rb]) - cwidth
                u1 := countpp == 1 ? upl : u1
                d1 := countpp == 1 ? dnl : d1
                tmp = array.new_bool(41, true)
                cnt = 0
                tpoint = 0
                for xx = 0 to prd
                    if na(close[xx])
                        break
                    if not na(ph[xx]) or not na(pl[xx])
                        chg = false
                        cnt += 1
                        if cnt > 40
                            break
                        if array.get(aas, cnt)
                            if not na(ph[xx])
                                if high[xx + rb] <= upl and high[xx + rb] >= dnl
                                    tpoint += 1
                                    chg := true
                            if not na(pl[xx])
                                if low[xx + rb] <= upl and low[xx + rb] >= dnl
                                    tpoint += 1
                                    chg := true
                        if chg and cnt < 41
                            array.set(tmp, cnt, false)
                if tpoint >= strengthSR
                    for g = 0 to 40 by 1
                        if not array.get(tmp, g)
                            array.set(aas, g, false)
                    if ph[x] and countpp < 21
                        array.set(sr_levels, countpp, high[x + rb])
                    if pl[x] and countpp < 21
                        array.set(sr_levels, countpp, low[x + rb])
// Plot
var line highest_ = na, line.delete(highest_)
var line lowest_  = na, line.delete(lowest_)
var line highest_fill1 = na, line.delete(highest_fill1)
var line highest_fill2 = na, line.delete(highest_fill2)
var line lowest_fill1  = na, line.delete(lowest_fill1)
var line lowest_fill2  = na, line.delete(lowest_fill2)
hi_col = close >= highestph ? colorSup : colorRes
lo_col = close >= lowestpl  ? colorSup : colorRes
if enableSR
    highest_ := line.new(bar_index - 311, highestph, bar_index, highestph, xloc.bar_index, expandSR ? extend.both : extend.right, hi_col, style, lineWidth)
    lowest_  := line.new(bar_index - 311, lowestpl , bar_index, lowestpl , xloc.bar_index, expandSR ? extend.both : extend.right, lo_col, style, lineWidth)
    if useHLZones
        highest_fill1 := line.new(bar_index - 311, highestph + zonePerc, bar_index, highestph + zonePerc, xloc.bar_index, expandSR ? extend.both : extend.right, na)
        highest_fill2 := line.new(bar_index - 311, highestph - zonePerc, bar_index, highestph - zonePerc, xloc.bar_index, expandSR ? extend.both : extend.right, na)
        lowest_fill1  := line.new(bar_index - 311, lowestpl + zonePerc , bar_index, lowestpl + zonePerc , xloc.bar_index, expandSR ? extend.both : extend.right, na)
        lowest_fill2  := line.new(bar_index - 311, lowestpl - zonePerc , bar_index, lowestpl - zonePerc , xloc.bar_index, expandSR ? extend.both : extend.right, na)
        linefill.new(highest_fill1, highest_fill2, color.new(hi_col, 80))
        linefill.new(lowest_fill1 , lowest_fill2 , color.new(lo_col, 80))
if ph or pl
    for x = 0 to array.size(sr_lines) - 1
        array.set(sr_levs, x, array.get(sr_levels, x))
for x = 0 to array.size(sr_lines) - 1
    line.delete(array.get(sr_lines, x))
    line.delete(array.get(sr_linesH, x))
    line.delete(array.get(sr_linesL, x))
    linefill.delete(array.get(sr_linesF, x))
    if array.get(sr_levs, x) and enableSR
        line_col = close >= array.get(sr_levs, x) ? colorSup : colorRes
        array.set(sr_lines, x, line.new(bar_index - 355, array.get(sr_levs, x), bar_index, array.get(sr_levs, x), xloc.bar_index, expandSR ? extend.both : extend.right, line_col, style, lineWidth))
        if useZones
            array.set(sr_linesH, x, line.new(bar_index - 355, array.get(sr_levs, x) + zonePerc, bar_index, array.get(sr_levs, x) + zonePerc, xloc.bar_index, expandSR ? extend.both : extend.right, na))
            array.set(sr_linesL, x, line.new(bar_index - 355, array.get(sr_levs, x) - zonePerc, bar_index, array.get(sr_levs, x) - zonePerc, xloc.bar_index, expandSR ? extend.both : extend.right, na))
            array.set(sr_linesF, x, linefill.new(array.get(sr_linesH, x), array.get(sr_linesL, x), color.new(line_col, 80)))
for x = 0 to array.size(sr_labels) - 1
    label.delete(array.get(sr_labels, x))
    if array.get(sr_levs, x) and enableSR
        lab_loc = close >= array.get(sr_levs, x) ? label.style_label_up : label.style_label_down
        lab_col = close >= array.get(sr_levs, x) ? colorSup             : colorRes
        array.set(sr_labels, x, label.new(bar_index + label_loc, array.get(sr_levs, x), str.tostring(math.round_to_mintick(array.get(sr_levs, x))), color=lab_col , textcolor=#000000, style=lab_loc))
hlabel := enableSR ? label.new(bar_index + label_loc + math.round(math.sign(label_loc)) * 20, highestph, "High Level : " + str.tostring(highestph), color=hi_col, textcolor=#000000, style=label.style_label_down) : na
llabel := enableSR ? label.new(bar_index + label_loc + math.round(math.sign(label_loc)) * 20, lowestpl , "Low  Level : " + str.tostring(lowestpl) , color=lo_col, textcolor=#000000, style=label.style_label_up  ) : na
